const jwt = require("jsonwebtoken");
const config = require("../config/jwt.config");

function createToken(id) {
  return jwt.sign({ id }, config.jwtSecret, { expiresIn: config.expiresIn });
}

function verifyToken(token) {
  try {
    return jwt.verify(token, config.jwtSecret);
  } catch (error) {
    console.log(error, error.name, "==============12");
    if (error.name === "TokenExpiredError") {
      return {
        message: "token expired",
        code: 40001,
      };
    } else if (error.name === "JsonWebTokenError") {
      return {
        message: "invalid token",
        code: 40001,
      };
    }
  }
}

module.exports = { createToken, verifyToken };
